package com.zc.mychart.model;

import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;

import java.util.ArrayList;
import java.util.List;

public class BuySellSignalEntrys {

    /**
     * AA: 20日内最低价的最低值
     * BB: 50日内最高价的最高值
     * 趋势线: (收盘价-AA)/(BB-AA)*4的4日指数移动平均
     */

    private final int N = 20;
    private final int M = 50;

    private List<Entry> mRsvList;
    private List<Entry> mSignalList;

    public BuySellSignalEntrys(List<CandleEntry> list) {
        mSignalList = new ArrayList<>();
        mRsvList = new ArrayList<>();
        float rsv = 0;
        if (list != null && list.size() > 0) {
            float high;
            float low;
            for (int i = M - 1; i < list.size(); i++) {
                Float[] mhl = getHighAndLowBy(i, M, list);
                Float[] nhl = getHighAndLowBy(i, N, list);
                high = mhl[0];
                low = nhl[1];
                rsv = (list.get(i).getClose() - low) / (high - low) * 4;
                mRsvList.add(new Entry(list.get(i).getX(), rsv));
            }
            mSignalList = getMAEntries(mRsvList, 4);
        }
    }

    /**
     * 得到某区间内最高价和最低价
     *
     * @param end 结束位置
     */
    private Float[] getHighAndLowBy(int end, int maxIndex, List<CandleEntry> list) {
        CandleEntry candleEntry = list.get(end);
        float high = candleEntry.getHigh();
        float low = candleEntry.getLow();
        Float[] wrs = new Float[2];
        for (int j = 0; j < maxIndex; j++) {
            candleEntry = list.get(end - j);
            high = high > candleEntry.getHigh() ? high : candleEntry.getHigh();
            low = low < candleEntry.getLow() ? low : candleEntry.getLow();
        }
        wrs[0] = high;
        wrs[1] = low;
        return wrs;
    }


    private List<Entry> getMAEntries(List<Entry> list, int n) {
        List<Entry> entries = new ArrayList<>();
        if (list != null) {
            for (int i = n - 1; i < list.size(); i++) {
                float sum = 0;
                for (int j = 0; j < n; j++) {
                    sum += list.get(i - j).getY();
                }
                Entry entry = new Entry(list.get(i).getX(), sum / n);
                entries.add(entry);
            }
        }
        return entries;
    }

//    private List<Entry> getMAEntries(List<CandleEntry> list, int n) {
//        List<Entry> entries = new ArrayList<>();
//        if (list != null) {
//            for (int i = n - 1; i < list.size(); i++) {
//                float sum = 0;
//                for (int j = 0; j < n; j++) {
//                    sum += list.get(i - j).getClose();
//                }
//                Entry entry = new Entry(list.get(i).getX(), sum / n);
//                entries.add(entry);
//            }
//        }
//        return entries;
//    }

    public List<Float> getEMArsv(List<Float> list) {
        List<Float> rsvList = new ArrayList<>();
        float ema = 0;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                float rsv = list.get(i);
                if (i == 0) {
                    ema = rsv;
                } else {
                    ema = (2 * rsv + (M - 1) * ema) / (M + 1);
                }
                rsvList.add(ema);
            }
        }
        return rsvList;
    }


    public List<Float> getEMAK(List<Float> list) {
        List<Float> p = new ArrayList<>();
        float ema = 0;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                float rsv = list.get(i);
                if (i == 0) {
                    ema = rsv;
                } else {
                    ema = (2 * rsv + (M - 1) * ema) / (M + 1);
                }
                p.add(ema);
            }
        }
        return p;
    }


//    public List<Float> getMAD(List<Float> list) {
//        if (list != null) {
//            for (int i = M; i < list.size(); i++) {
//                float sum = 0;
//                for (int j = 0; j < M; j++) {
//                    sum += list.get(i - j);
//                }
//                mDList.add(sum / M);
//            }
//        }
//        return mDList;
//    }

    public List<Entry> getSignalList() {
//        for (int i = 0; i < mRsvList.size(); i++) {
//            Log.e("art", "index : " + i + ", rsv : " + mRsvList.get(i));
//        }

//        for (int i = 0; i < Ks.size(); i++) {
//            Log.e("art", "index : " + i + ", K : " + Ks.get(i));
//        }
        return mSignalList;
    }


}
